package com.airticketbooking.controller.user;

import com.airticketbooking.common.domain.R;

import com.airticketbooking.common.utils.UserContext;
import com.airticketbooking.dto.UserDTO;
import com.airticketbooking.po.User;
import com.airticketbooking.vo.UserVO;
import com.airticketbooking.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.math.BigDecimal;

/**
 * @author Mr.J
 * @version 1.0.0
 * @date 2024/10/12
 * @desc
 */
@RequestMapping("air/user/user")
@RestController
@Api(tags = "用户端用户相关接口")
@RequiredArgsConstructor
@Slf4j

public class UserController {


	private final UserService userService;

//用户登录接口

	@GetMapping("/login")
	@ApiOperation(value = "用户登录接口")
	public R<UserVO> login(String username, String password) {


		try {
			UserVO userVO = userService.login(username, password);

			return R.ok(userVO);
		} catch (Exception e) {
			return R.error(e.getMessage());
		}


	}

	//	用户注册接口
	@PostMapping("/register")
	@ApiOperation(value = "用户注册接口")
	public R<String> register(
			@NotBlank(message = "用户名不能为空")
			@Size(min = 3, max = 20, message = "用户名长度必须在3到20个字符之间")
			String username,

			@NotBlank(message = "密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String password) {

		try {
			userService.register(username, password);
			return R.ok("注册成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}


	}

	//	用户修改密码接口
	@GetMapping("/changePassword")
	@ApiOperation(value = "用户修改密码接口")
	public R<String> changePassword(

			@NotBlank(message = "旧密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String oldPassword,

			@NotBlank(message = "新密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String newPassword,


			@NotBlank(message = "新密码不能为空")
			@Size(min = 6, max = 20, message = "密码长度必须在6到20个字符之间")
			String rePassword) {

		try {
			userService.changePassword(oldPassword, newPassword, rePassword);

			return R.ok("密码修改成功");

		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}


//	修改用户数据接口

	@PutMapping("/updateUserInfo")
	@ApiOperation(value = "修改用户数据接口")
	public R<String> updateUserInfo(
			@RequestBody
			UserDTO userDTO) {


		try {
			userService.updateUserInfo(userDTO);
			return R.ok("用户信息修改成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}


	}


	//获取用户信息接口
	@GetMapping("/getUserInfo")
	@ApiOperation(value = "获取用户信息接口")
	public R<User> getUserInfo() {

		User user = userService.getUserById(UserContext.getUser().intValue());

		return R.ok(user);

	}

	//	更新用户头像接口
	@GetMapping("/updateAvatar")
	@ApiOperation(value = "更新用户头像接口")
	public R<String> updateUserAvatar(
			String avatarUrl) {

		try {
			userService.updateUserAvatar(avatarUrl);
			return R.ok("用户头像修改成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}

	}

	//	账号充值接口
	@GetMapping("/recharge")
	@ApiOperation(value = "账号充值接口")
	public R<String> recharge(
			double amount) {

		try {

			BigDecimal amountInBigDecimal = new BigDecimal(amount);

			userService.recharge(amountInBigDecimal);
			return R.ok("充值成功");
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}


}
